package com.zijidelu.luframework.application.domain;

import com.zijidelu.luframework.application.domain.event.DefaultDomainEventPublisher;
import com.zijidelu.luframework.base.model.DataModel;
import com.zijidelu.luframework.domain.model.aggregate.AggregateRoot;
import com.zijidelu.luframework.domain.model.entity.Entity;
import com.zijidelu.luframework.domain.repository.AggregateRepository;
import com.zijidelu.luframework.domain.service.DomainService;
import lombok.Setter;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 基础领域服务抽象类。
 *
 * @author ZIJIDELU
 * @datetime 2025/10/28 16:47
 */
public abstract class AbstractDomainService<D extends DataModel, E extends Entity<D>, A extends AggregateRoot<D, E>> implements DomainService {
    @Setter(onMethod_ = {@Autowired})
    private DefaultDomainEventPublisher defaultDomainEventPublisher;
    @Setter(onMethod_ = {@Autowired})
    private ObjectProvider<AggregateRepository<D, E, A>> aggregateRepositoryProvider;
    private AggregateRepository<D, E, A> aggregateRepository;

    protected DefaultDomainEventPublisher getDefaultDomainEventPublisher() {
        return defaultDomainEventPublisher;
    }

    protected AggregateRepository<D, E, A> getAggregateRepository() {
        if (aggregateRepository == null) {
            aggregateRepository = aggregateRepositoryProvider.getIfAvailable();
        }

        return aggregateRepository;
    }
}
